﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class PageAdorner : Adorner
    {
        /// <summary>
        /// 用在整页指示标尺的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入主窗口的mainCanvas。</param>
        public PageAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            mainCanvas = adornedElement as Canvas;
            this.Opacity = 0.5;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(PageAdorner_MouseLeftButtonDown);
        }

        void PageAdorner_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                Globals.MainWindow.isPageRulerVisible = false;
                Globals.MainWindow.RefreshPageAdorner();
            }
        }

        private Canvas mainCanvas;

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (Globals.MainWindow.isPageRulerVisible == false) return;

            ToolTip = "页面标尺：" + Globals.MainWindow.PageSetting.PaperTypeName + " <双击关闭>";

            #region 原页面标尺
            //Rect srcRectOfCanvas = new Rect(this.AdornedElement.RenderSize);

            //Rect pageRect;

            //if (Globals.MainWindow.PageDirection == Orientation.Horizontal)
            //{
            //    pageRect = new Rect(
            //        (srcRectOfCanvas.Width - Globals.MainWindow.PageSetting.LongSideWPFUnit) / 2,
            //        (srcRectOfCanvas.Height - Globals.MainWindow.PageSetting.ShortSideWPFUnit) / 2,
            //        Globals.MainWindow.PageSetting.LongSideWPFUnit,
            //        Globals.MainWindow.PageSetting.ShortSideWPFUnit);
            //}
            //else
            //{
            //    pageRect = new Rect(
            //        (srcRectOfCanvas.Width - Globals.MainWindow.PageSetting.ShortSideWPFUnit) / 2,
            //        (srcRectOfCanvas.Height - Globals.MainWindow.PageSetting.LongSideWPFUnit) / 2,
            //        Globals.MainWindow.PageSetting.ShortSideWPFUnit,
            //        Globals.MainWindow.PageSetting.LongSideWPFUnit);
            //}

            //Brush renderBrush = Brushes.DarkOliveGreen;
            //Pen renderPen = new Pen(renderBrush, 10);
            //renderPen.DashStyle = DashStyles.DashDotDot;

            //drawingContext.DrawRectangle(null, renderPen, pageRect); 
            #endregion

            PaperInfo curPaperInfo = Globals.MainWindow.PageSetting;
            if (curPaperInfo == null) return;

            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = Brushes.DarkOliveGreen;

            Pen renderPen;

            renderPen = new Pen(renderBrush, 10)
            {
                EndLineCap = PenLineCap.Square,
                DashStyle = DashStyles.DashDotDot,
            };

            double x, y;
            switch (Globals.MainWindow.PageDirection)
            {
                case Orientation.Horizontal:
                    {
                        x = curPaperInfo.LongSideWPFUnit; y = curPaperInfo.ShortSideWPFUnit; break;
                    }
                default:
                    {
                        x = curPaperInfo.ShortSideWPFUnit; y = curPaperInfo.LongSideWPFUnit; break;
                    }
            }

            Thickness pageMargin = new Thickness(20);//TODO: 注意，这里应和导出切片图像时使用的页面边距一致。

            //再考虑页边距
            //x -= (masterEditor.PageSettingInfo.MarginLeft + masterEditor.PageSettingInfo.MarginRight);
            //y -= (masterEditor.PageSettingInfo.MarginTop + masterEditor.PageSettingInfo.MarginBottom);
            x -= (pageMargin.Left + pageMargin.Right);
            y -= (pageMargin.Top + pageMargin.Bottom);

            double realWidth = x;
            double realHeight = y;

            while (x < rect.Width)
            {
                drawingContext.DrawLine(renderPen,
                    new Point(x, 0),
                    new Point(x, rect.Height)
                    );
                x += realWidth;
            }

            while (y < rect.Height)
            {
                drawingContext.DrawLine(renderPen,
                      new Point(0, y),
                      new Point(rect.Width, y)
                      );
                y += realHeight;
            }
        }
    }
}